<template>
  <el-form :rules="rules" label-width="80px" :model="cForm" ref="lv2Form">
    <el-form-item label="父级信息">
      <p class="parent-lv1">{{parentLv1.class1name}}({{parentLv1.class1}})</p>
    </el-form-item>
    <el-form-item label="分类ID" prop="class2">
      <el-input v-model="cForm.class2" :disabled="isEdit"></el-input>
    </el-form-item>
    <el-form-item label="分类名" prop="class2name">
      <el-input v-model="cForm.class2name"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="explains">
      <el-input type="textarea" :rows="3" v-model="cForm.explains"></el-input>
    </el-form-item>
    <!-- <el-form-item label="分类图片" prop="class2image">
      <upload-image :src="src" :upload="handleUpload" size="small"></upload-image>
    </el-form-item> -->
    <el-form-item>
      <el-button type="primary" @click="submit">提交</el-button>
      <el-button plain @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import UploadImage from './UploadImage.vue'
  import { uploadPicToDir, addClass2, updateClass2 } from '../service/api'
  import { mapState, mapActions, mapMutations } from 'vuex'
  export default {
    name: 'lv2-form-editing',
    props: ['form', 'reset', 'isEdit'],
    components: { UploadImage },
    data () {
      return {
        rules: {
          class2: [{ required: true }],
          // class2image: [{ required: true }],
          explains: [{ required: true }],
          class2name: [{ required: true }]
        },
        lv1Class: this.$route.query.id,
        cForm: { ...this.form }
      }
    },
    computed: {
      ...mapState('ci', ['lv2', 'lv1']),
      src () {
        return this.$apath + 'group/' + this.cForm.class2image
      },
      parentLv1 () {
        return this.lv1.find(v => v.class1 === this.lv1Class)
      }
    },
    methods: {
      ...mapActions('ci', ['fetchLv2']),
      ...mapMutations('ci', ['setLv2Expired']),
      /* 处理图片 */
      async handleUpload (file) {
        const { data } = await uploadPicToDir({ photo: file, filePath: this.$upath + 'group/' })
        if (!data || +data.code !== 200) {
          this.$message.error('上传失败')
          return
        }
        this.cForm = { ...this.cForm, class2image: data.result }
      },
      /* 提交数据 */
      submit () {
        this.$refs['lv2Form'].validate(valid => {
          if (!valid) return
          this.handleSubmit()
        })
      },
      async handleSubmit () {
        const { data } =
          this.isEdit
            ? await updateClass2(this.cForm)
            : await addClass2({ ...this.cForm, ...this.parentLv1 })
        if (!data || +data.code !== 200) {
          this.$message.error('操作失败')
          return
        }
        this.$message.success('操作成功')
        this.cancel()
        // @TODO 更新store
        this.setLv2Expired({ c1: this.lv1Class })
        this.fetchLv2({ c1: this.lv1Class })
      },
      cancel () {
        this.$emit('close')
        this.$refs['lv2Form'].resetFields()
      }
    },
    watch: {
      'form.class2' () {
        this.$refs['lv2Form'].resetFields()
        this.cForm = { ...this.form }
      }
      // reset () {
      //   console.log(1)
      // this.$refs['lv2Form'].resetFields()
      // }
    }
  }
</script>

<style lang="scss" scoped>
  .parent-lv1 {
    font-size: 1.2rem;
    text-align: center;
  }
</style>

